# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\Crypto\Random\OSRNG\nt.py
__revision__ = "$Id$"
__all__ = ["WindowsRNG"]
from . import winrandom
from .rng_base import BaseRNG


class WindowsRNG(BaseRNG):
    name = "<CryptGenRandom>"

    def __init__(self):
        self._WindowsRNG__winrand = winrandom.new()
        BaseRNG.__init__(self)

    def flush(self):
        """Work around weakness in Windows RNG.
        
        The CryptGenRandom mechanism in some versions of Windows allows an
        attacker to learn 128 KiB of past and future output.  As a workaround,
        this function reads 128 KiB of 'random' data from Windows and discards
        it.
        
        For more information about the weaknesses in CryptGenRandom, see
        _Cryptanalysis of the Random Number Generator of the Windows Operating
        System_, by Leo Dorrendorf and Zvi Gutterman and Benny Pinkas
        http://eprint.iacr.org/2007/419
        """
        if self.closed:
            raise ValueError("I/O operation on closed file")
        data = self._WindowsRNG__winrand.get_bytes(131072)
        if not len(data) == 131072:
            raise AssertionError
        BaseRNG.flush(self)

    def _close(self):
        self._WindowsRNG__winrand = None

    def _read(self, N):
        self.flush()
        data = self._WindowsRNG__winrand.get_bytes(N)
        self.flush()
        return data


def new(*args, **kwargs):
    return WindowsRNG(*args, **kwargs)
